package com.dofast.bt.base;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.dofast.bt.util.Helper;

import butterknife.ButterKnife;


/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：
 */

public abstract class BaseMvpActivity<P extends Presenter<V>, V extends BaseMvpView> extends AppCompatActivity implements BaseMvpView, LoaderManager.LoaderCallbacks<P> {
    private final int BASE_LODER_ID = 1000;//loader的id值
    private ProgressDialog progressDialog;//登录进度条
    protected P presenter;

    protected abstract int getResourcesId();

    protected abstract void initView();


    protected abstract void addOnClickListener();

    private boolean isFirst = true;

    //广播
    private MyBroadcastReceiver receiver = null;

    private IntentFilter filter = null;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(getResourcesId());
        BaseApplication.getLogger().i("info", getClass().getSimpleName());
        progressDialog = new ProgressDialog(this);//实例化progressDialog
        getSupportLoaderManager().initLoader(BASE_LODER_ID, null, this);//初始化loader
        ButterKnife.bind(this);
        filter  = new IntentFilter();
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.attachView((V) this);//presenter与view 连接
        if (isFirst) {
            initView();
            addOnClickListener();
            isFirst = false;
        }

    }

    @Override
    public void showLoading(String msg) {
        progressDialog.setMessage(msg);//设置进度条加载内容
        progressDialog.setCancelable(true);//设置是否可以通过点击Back键取消
        progressDialog.setCanceledOnTouchOutside(false);//设置在点击Dialog外是否取消Dialog进度条
        if (!progressDialog.isShowing())//如果进度条没有显示
            progressDialog.show();//显示进度条
    }

    @Override
    public void hideLoading() {
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }

    @Override
    public void showErr(String err) {
        Helper.showToast(err);
    }


    @Override
    public void onLoadFinished(Loader<P> loader, P data) {
        presenter = data;
    }

    @Override
    public void onLoaderReset(Loader<P> loader) {
        presenter = null;
    }
    @Override
    protected void onDestroy() {
        presenter.detachView();//presenter与view 断开连接
        super.onDestroy();


    }

    protected void startActivity(Class<?> tClass, boolean isFinish) {
        Intent intent = new Intent(this, tClass);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    /**
     * @param tClass   目标activity
     * @param bundle   携带数据
     * @param isFinish true 表示结束本界面
     */
    protected void startActivity(Class<?> tClass, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(this, tClass);
        intent.putExtras(bundle);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    protected void startActivity(Class<?> tClass) {
        startActivity(tClass, false);
    }

    /**
     * 广播
     */
    private class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }

}

